﻿using System.Data.Common;
using System.Runtime.InteropServices;
using NetOffice.OfficeApi;
using NetOffice.OfficeApi.Enums;
using NetOffice.Tools;
using NetOffice.VBIDEApi;
using NetOffice.VBIDEApi.Tools;

namespace 枸杞VBE插件.ComHost
{
    [ComVisible(true)]
    [Guid("BAD8895B-62A1-4B0B-B2A8-3C649F474F64")]
    [ProgId("枸杞VBE插件.Core")]
    [COMAddin("枸杞VBE插件.Core", "QQ群:1007293806", LoadBehavior.LoadAtStartup)]
    public partial class ThisAddin : COMAddin
    {
        public ThisAddin()
        {
            OnConnection += ThisAddin_OnConnection;
            OnStartupComplete += ThisAddin_OnStartupComplete;
            OnDisconnection += ThisAddin_OnDisconnection;
            OnBeginShutdown += ThisAddin_OnBeginShutdown;
        }

        #region COMAddin

        private void ThisAddin_OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            VBE = Application;
            Instance = this;
            Addin = new AddIn(null, addInInst)
            {
                Object = this
            };
            if (connectMode != ext_ConnectMode.ext_cm_Startup)
            {
                ThisAddin_OnStartupComplete(ref custom); // 用户手动加载时不触发OnStartupComplete,强制触发
            }
        }

        private void ThisAddin_OnStartupComplete(ref Array custom)
        {
            Startup();
        }

        private void ThisAddin_OnDisconnection(ext_DisconnectMode removeMode, ref Array custom)
        {
            if (removeMode != ext_DisconnectMode.ext_dm_HostShutdown)
                ThisAddin_OnBeginShutdown(ref custom);//用户手动取消加载时不触发OnBeginShutdown,强制触发
        }

        private void ThisAddin_OnBeginShutdown(ref Array custom)
        {
            Shutdow();
        }

        #endregion COMAddin

        public VBE? VBE { get; set; }
        public static ThisAddin Instance { get; set; } = null!;
        internal AddIn? Addin;
        internal CommandBar? MainMenu;
        internal CommandBar? CodeWindowMenu;
        internal int controlIndex = 1;
        internal Dictionary<string, CommandBarControl?> ControlDictionary = [];

        public void Startup()
        {
            GenerateCodeWindowMenu();
            GenerateMainMenu();
            //AddPrimaryMenu();
            //AddSecondaryControls();

            //Thread thread = new Thread(() =>
            //{
            //    var UI = new MainWindow();
            //    var Helper = new WindowInteropHelper(UI);
            //    Helper.Owner = ExcelHWND;
            //    UI.Show();
            //    UI.Closed += (a, b) => { UI.Dispatcher.InvokeShutdown(); };
            //    Dispatcher.Run();
            //})
            //{ IsBackground = true };
            //thread.SetApartmentState(ApartmentState.STA);
            //thread.Start();
        }

        internal void GenerateCodeWindowMenu()
        {
            CodeWindowMenu = VBE?.CommandBars["Code Window"];
            CodeWindowMenu?.Controls.AddButton("删除空行", 3411, Commands.删除空行).Move(null, 1);
            //CodeWindowMenu.Controls.AddButton("格式化当前模块", 3495, 格式化当前模块).Move(null, 1).BeginGroup = true;
        }

        internal void AddSecondaryControls()
        {
            throw new NotImplementedException();
        }

        internal void AddPrimaryMenu()
        {
            throw new NotImplementedException();
        }

        internal void GenerateMainMenu()
        {
            if (MainMenu == null)
            {
                MainMenu = VBE?.CommandBars.Add("枸杞VBE工具栏", MsoBarPosition.msoBarTop, null, true);
                ControlDictionary["枸杞VBE工具栏"] = null;
                if (MainMenu != null)
                {
                    MainMenu.Enabled = true;
                    MainMenu.Visible = true;
                }
                //主菜单生成基础功能按钮();
            }
        }

        public void Shutdow()
        {
        }
    }
}